#include<iostream>
#include<cstring>
using namespace std;
const int MAXN=10;
int arc[MAXN][MAXN];
int vertex[MAXN];
int visited[MAXN];
int n,e;
int i,j,k;

void CreateMGraph()
{
    cin>>n>>e;
    for(i=0; i<n; i++)
        for(j=0; j<n; j++)
            arc[i][j]=0;

    memset(visited,0,sizeof(visited));
    for(i=0; i<n; i++)
        vertex[i]=i;
    for(k=0; k<e; k++)
    {
        cin>>i>>j;
        arc[i][j]=1;
        //arc[j][i]=1;
    }
}

void print()
{
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            cout<<arc[i][j]<<" ";
        }
        cout<<endl;
    }
}
void DFSTraverse(int v)
{
    int a[MAXN],i,top;
    top=-1;
    cout<<vertex[v];
    visited[v]=1;
    a[++top]=v;
    while(top!=-1)
    {
        v=a[top];
        for(i=0; i<n; i++)
            if(arc[v][i]==1&&visited[i]!=1)
            {
                cout<<vertex[i];
                visited[i]=1;
                a[++top]=i;
                break;
            }
        if(i==n)
            top--;
    }
}

int main()
{
    CreateMGraph();
    print();
    DFSTraverse(0);
    return 0;
}
/*
7 7
0 1
0 2
1 3
1 4
2 5
2 6
5 6
*/
